/**
 * 
 */
package com.presalept.manipulatedata;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

import com.presalept.polodo.ProjectDO;
import com.presalept.polodo.ProjectMemberDO;

/**
 * @author 
 *
 */
public class CreateProject {
	
	//insert project 
	
	public static int InsertProject(ProjectDO projectdo) throws SQLException{
		
		Connection dbConn = null;
		PreparedStatement dbPreStmt = null;
		PreparedStatement dbPreUserStmt = null;
		ResultSet dbRs = null;
		int insertProjectResult = 0;
		int[] insertProjectUserResult;
		int InsertResult = 0;
		
		String dbProjectSql = "INSERT INTO project (projectname,projectdescription,clientidofproject,createrid,createdate,updatestatus,lastupdatecontent,lastupdatedate)" 
					+" VALUES(?,?,?,?,?,?,?,?); ";
		
		String dbProjectUserSql = "INSERT INTO relationofprojectanduser (projectid,userid,purelationstatus,createdate)"
				+ " VALUES(?,?,?,?); ";
		
		try{
		
			dbConn = DbManager.getConnection();		
			dbConn.setAutoCommit(false);
			
			dbPreStmt = dbConn.prepareStatement(dbProjectSql);
			dbPreUserStmt = dbConn.prepareStatement(dbProjectUserSql);
					
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");  
	        Date createdate = null;  
	        Date updatedate = null;
	        try {  
	        	createdate = sdf.parse(projectdo.getCreateDate()); 
	        	updatedate = sdf.parse(projectdo.getLastupdatedate()); 
	            
	        } catch (ParseException e) {  
	            e.printStackTrace();  
	        }       
	        java.sql.Date sqlcreateDate = new java.sql.Date(createdate.getTime()); 
	        java.sql.Date sqlupdateDate = new java.sql.Date(updatedate.getTime()); 	
	        
	        //insert one record into project table
	        
			dbPreStmt.setString(1, projectdo.getProjectName());
			dbPreStmt.setString(2, projectdo.getProjectDescription());
			dbPreStmt.setInt(3, projectdo.getClientidofproject());
			dbPreStmt.setInt(4, projectdo.getCreaterId());
			dbPreStmt.setDate(5, sqlcreateDate);
			dbPreStmt.setInt(6, projectdo.getUpdatestatus());
			dbPreStmt.setString(7, projectdo.getLastupdatecontent());
			dbPreStmt.setDate(8, sqlupdateDate);
			insertProjectResult = dbPreStmt.executeUpdate(dbProjectSql);	
			
			
			// insert records into relationofprojectanduser table
			
			if(insertProjectResult == 1){
				dbRs = dbPreStmt.getGeneratedKeys();
				int CurrentProjectId = dbRs.getInt(1);
				for(Iterator<ProjectMemberDO> i = projectdo.getProjectMember().iterator(); i.hasNext();){
					
					dbPreUserStmt.setInt(1, CurrentProjectId);
					dbPreUserStmt.setInt(2, i.next().getUserId());
					dbPreUserStmt.setInt(3, 1);
					dbPreUserStmt.setDate(4, sqlcreateDate);
					
					dbPreUserStmt.addBatch();			
				}
				
				// batch insert
				
				insertProjectUserResult = dbPreUserStmt.executeBatch();
				boolean isInsertRight = false;
				for(int i = 0; i < insertProjectUserResult.length; i++){
					
					//if some record has insert error, then rollback
					
					if(insertProjectUserResult[i] == 0){
						dbConn.rollback();
						break;
					}
					isInsertRight = true;
				}
				if(isInsertRight){
					dbConn.commit();
					InsertResult = 1;
				}
			}			
		}finally{
			if (dbRs != null) dbRs.close();
			if (dbConn != null) dbConn.close();			
		}
		return InsertResult;
	}
}
